package com.hhh.Controller;

import com.hhh.Pojo.Result;
import com.hhh.Pojo.User;
import com.hhh.Service.UserSerivce;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserContorller {
    @Autowired
    UserSerivce userSerivce;

    //    手机端登录
    @PostMapping("/login")
    public Result Login(@RequestBody User user, HttpServletRequest request) {
        log.info("客户登录");
        User returnuser = userSerivce.Select(user);
        if (returnuser == null)
            userSerivce.Insert(user);
        returnuser = userSerivce.Select(user);
        request.getSession().setAttribute("user", returnuser.getId());
        return Result.success("登录成功");
    }

    @PostMapping("/loginout")
    public Result LoginOut(HttpServletRequest request) {
        log.info("客户登出");
        request.getSession().removeAttribute("user");
        return Result.success("客户登出");
    }
}
